package hos.ui.radio;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;

import hos.ui.R;
import hos.ui.helper.ShapeHelper;
import hos.ui.helper.TextColorHelper;
import hos.ui.inter.OnClickableListener;

/**
 * <p>Title: RadioGroupSelected </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/8/3 20:35
 */
public class RadioGroupExtend extends RadioGroup implements OnClickableListener {

    private RadioButton mCheckRadio;
    private int mCheckPosition;
    private int mRadioType;
    private ShapeHelper.DrawableType shapeDrawable;
    private ColorStateList textColorStateList;
    private int textSize;

    public RadioGroupExtend(Context context) {
        this(context, HORIZONTAL);
    }

    public RadioGroupExtend(Context context, int orientation) {
        this(context, null);
        setOrientation(orientation);
    }

    public RadioGroupExtend(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttributeSet(context, attrs);
        defaultCheckChange();
    }

    public void initAttributeSet(Context context, AttributeSet attrs) {
        TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.RadioGroupExtend);
        try {
            final CharSequence[] entries = typedArray.getTextArray(R.styleable.RadioGroupExtend_android_entries);
            mRadioType = typedArray.getInteger(R.styleable.RadioGroupExtend_radioType, -1);
            shapeDrawable = ShapeHelper.loadFromAttributes(this, attrs);
            textColorStateList = TextColorHelper.loadFromAttributes(this, attrs);
            textSize = typedArray.getDimensionPixelSize(R.styleable.RadioGroupExtend_android_textSize, -1);
            addArray(entries);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void clearCheck() {
        super.clearCheck();
        mCheckRadio = null;
        mCheckPosition = -1;
        checkPosition(0);
    }

    public void checkPosition(int position) {
        int childCount = getChildCount();
        if (childCount > position) {
            mCheckRadio = (RadioButton) getChildAt(position);
            check(mCheckRadio.getId());
            mCheckPosition = position;
        }
    }

    public void checkText(CharSequence text) {
        if (text == null) {
            return;
        }
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            if (childAt instanceof RadioButton) {
                RadioButton child = (RadioButton) childAt;
                if (TextUtils.equals(child.getText(), text)) {
                    mCheckRadio = child;
                    check(mCheckRadio.getId());
                    mCheckPosition = i;
                    break;
                }
            }
        }
    }

    public CharSequence getCheckText() {
        if (mCheckRadio != null) {
            return mCheckRadio.getText();
        }
        return null;
    }


    public RadioButton getCheckRadio() {
        return mCheckRadio;
    }

    public int getCheckPosition() {
        return mCheckPosition;
    }

    public void addList(List<CharSequence> textList, Drawable drawable) {
        addArray(textList.toArray(new CharSequence[0]), drawable);
    }

    public void addArray(CharSequence[] textList, Drawable drawable) {
        add(drawable, textList);
    }

    public void addList(List<CharSequence> textList) {
        addArray(textList.toArray(new CharSequence[0]));
    }

    public void addArray(CharSequence[] textList) {
        add(textList);
    }

    public void add(CharSequence... textList) {
        add(null, textList);
    }

    public void add(Drawable drawable, CharSequence... textList) {
        if (textList == null) {
            return;
        }
        removeAllViews();
        int length = textList.length;
        int checkId = 0;
        for (int i = 0; i < length; i++) {
            RadioButton button = generateRadioButton();
            if (textColorStateList != null) {
                button.setTextColor(textColorStateList);
            }
            if (textSize != -1) {
                button.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            }
            if (drawable != null) {
                button.setButtonDrawable(null);
                button.setPadding(24, 4, 24, 4);
                button.setBackground(drawable);
            } else {
                if (mRadioType == 0) {
                    if (shapeDrawable != null) {
                        button.setButtonDrawable(null);
                        button.setPadding(24, 4, 24, 4);
                        button.setBackground(shapeDrawable.newSource());
                    }
                } else if (mRadioType == 1) {
                    if (shapeDrawable != null) {
                        button.setButtonDrawable(null);
                        button.setPadding(24, 4, 24, 4);
                        // 连续的
                        if (i == 0) {
                            button.setBackground(shapeDrawable.newLeft());
                        } else if (i == length - 1) {
                            button.setBackground(shapeDrawable.newRight());
                        } else {
                            button.setBackground(shapeDrawable.newFull());
                        }
                    }
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                button.setId(generateViewId());
            } else {
                button.setId(i);
            }
            button.setText(textList[i]);
            button.setTag(i);
            LinearLayout.LayoutParams layoutParams = generateDefaultLayoutParams();
            if (mRadioType != 1) {
                if (shapeDrawable == null) {
                    if (i == 0) {
                        layoutParams.leftMargin = 12;
                    } else if (i == length - 1) {
                        layoutParams.rightMargin = 12;
                    } else {
                        layoutParams.leftMargin = 12;
                        layoutParams.rightMargin = 12;
                    }
                } else {
                    // 连续的
                    if (i == 0) {
                        layoutParams.leftMargin = shapeDrawable.shapeSpace;
                    } else if (i == length - 1) {
                        layoutParams.rightMargin = shapeDrawable.shapeSpace;
                    } else {
                        layoutParams.leftMargin = shapeDrawable.shapeSpace;
                        layoutParams.rightMargin = shapeDrawable.shapeSpace;
                    }
                }
            }
            addView(button, layoutParams);
            // 默认选中 第一个
            if (i == 0) {
                mCheckPosition = 0;
                mCheckRadio = button;
                checkId = button.getId();
            }
        }
        setTag(textList[0]);
        check(checkId);
    }

    public void setOnCheckedListener(OnCheckedListener listener) {
        if (listener == null) {
            setOnCheckedChangeListener(null);
            return;
        }
        defaultCheckChange(listener);
    }

    private void defaultCheckChange() {
        defaultCheckChange(null);
    }

    private void defaultCheckChange(OnCheckedListener listener) {
        setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int childCount = group.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    View childAt = group.getChildAt(i);
                    if (childAt.getId() == checkedId) {
                        mCheckPosition = i;
                        mCheckRadio = (RadioButton) childAt;
                        if (listener != null) {
                            listener.onCheckedChanged(RadioGroupExtend.this);
                        }
                        break;
                    }
                }
            }
        });
    }

    public interface OnCheckedListener {
        void onCheckedChanged(RadioGroupExtend group);
    }

    protected RadioButton generateRadioButton() {
        return new RadioButtonView(getContext());
    }

    public void clickable(boolean isClickable) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View childAt = getChildAt(i);
            if (childAt instanceof OnClickableListener) {
                ((OnClickableListener) childAt).clickable(isClickable);
            } else if (childAt instanceof ViewGroup) {
                final int childCountGroup = ((ViewGroup) childAt).getChildCount();
                for (int i1 = 0; i1 < childCountGroup; i1++) {
                    final View childAt1 = ((ViewGroup) childAt).getChildAt(i1);
                    if (childAt1 instanceof OnClickableListener) {
                        ((OnClickableListener) childAt1).clickable(isClickable);
                    }
                }
            }
        }
    }

    public void onDestroy() {
        clearCheck();
        setOnCheckedListener(null);
        mCheckRadio = null;
    }

}
